function [mg, Cg] = arith2geom(ma, Ca, t)
%ARITH2GEOM Arithmetic to geometric moments of asset returns.
% Transform moments associated with a simple Brownian motion into equivalent continuously-compounded
% moments associated with a geometric Brownian motion with a possible change in periodicity.
%
%		[mg, Cg] = arith2geom(ma, Ca);
%		[mg, Cg] = arith2geom(ma, Ca, t);
%
% Inputs:
%	ma - Arithmetic mean of asset-return data (n-vector).
%	Ca - Arithmetic covariance of asset-return data (n x n symmetric positive-semidefinite matrix).
%
% Optional Inputs:
%	t - Target period of geometric moments in terms of periodicity of arithmetic moments with
%		default value 1 (scalar).
%
% Outputs:
%	mg - Continuously-compounded or "geometric" mean of asset returns over the target period
%		(n-vector).
%	Cg - Continuously-compounded or "geometric" covariance of asset returns over the target period
%		(n x n matrix).
%
% Notes:
%	Arithmetic returns over period ta are modeled as multivariate normal random variables
%		X ~ N(ma, Ca)
%	with moments E[X] = ma and cov(X) = Ca .
%
%	Geometric returns over period tg are modeled as multivariate lognormal random variables
%		Y ~ LN(1 + mg, Cg)
%	with moments E[Y] = 1 + mg and cov(Y) = Cg .
%
%	The transformation from arithmetic to geometric moments is
%		1 + mg(i) = E[Y(i)] = exp(ma(i)*t + 0.5*Ca(i,i)*t))
%		Cg(i,j) = cov(Y(i),Y(j)) = E[Y(i)]*E[Y(j)]*(exp(Ca(i,j)*t) - 1)
%	for i, j = 1, ... , n with t = tg/ta. Note that if t = 1, then Y = exp(X).
%
%	This function has no restriction on the input mean ma but requires the input covariance Ca to be
%	a symmetric positive-semidefinite matrix.
%
%	The functions arith2geom and geom2arith are complementary so that, given m, C, and t, the
%	sequence
%		[mg, Cg] = arith2geom(m, C, t);
%		[ma, Ca] = geom2arith(mg, Cg, 1/t);
%	yields ma = m and Ca = C.
%
% See also GEOM2ARITH.

%	Copyright 2007 The MathWorks, Inc.
%	$Revision: 1.1.6.2 $ $Date: 2007/06/04 21:07:25 $

% Examples:
%	Given arithmetic mean m and covariance C of monthly total returns, obtain annual geometric mean
%	mg and covariance Cg. In this case, the output period (1 year) is 12 times the input period
%	(1 month) so that t = 12 with
%		[mg, Cg] = arith2geom(m, C, 12);
%
%	Given annual arithmetic mean m and covariance C of asset returns, obtain monthly geometric
%	mean mg and covariance Cg. In this case, the output period (1 month) is 1/12 times the input
%	period (1 year) so that t = 1/12 with
%		[mg, Cg] = arith2geom(m, C, 1/12);
%
%	Given arithmetic means m and standard deviations s of daily total returns (derived from 260
%	business days per year), obtain annualized continuously-compounded mean mg and standard
%	deviations sg with
%		[mg, Cg] = arith2geom(m, diag(s .^2), 260);
%		sg = sqrt(diag(Cg));
%
%	Given arithmetic mean m and covariance C of monthly total returns, obtain quarterly
%	continuously-compounded return moments. In this case, the output is 3 of the input periods so
%	that t = 3 with
%		[mg, Cg] = arith2geom(m, C, 3);
%
%	Given arithmetic mean m and covariance C of 1254 observations of daily total returns over
%	a 5-year period, obtain annualized continuously-compounded return moments. Since the periodicity
%	of the arithmetic data is based on 1254 observations for a 5-year period, a 1-year period for
%	geometric returns implies a target period of t = 1254/5 so that
%		[mg, Cg] = arith2geom(m, C, 1254/5);

if nargin < 2
	error('Finance:arith2geom:MissingInputArgument', ...
		'A required input argument is missing.');
end
if nargin < 3 || isempty(t)
	t = 1;
end

if ~isvector(ma) || ~isa(ma,'double')
	error('Finance:arith2geom:InvalidInputArg', ...
		'Input mean should be a vector.');
end
if ndims(Ca) ~= 2 || ~isa(Ca,'double')
	error('Finance:arith2geom:InvalidInputArg', ...
		'Input covariance should be a matrix.');
end
if ~isscalar(t) || ~isa(t,'double')
	error('Finance:arith2geom:InvalidInputArg', ...
		'Input target period should be a scalar.');
end

if size(ma,1) < size(ma,2)
	flip = true;
	ma = ma(:);
else
	flip = false;
end

if ~all(size(Ca) == size(ma,1))
	error('Finance:arith2geom:NonConformableInputs', ...
		'Non-conformable mean and covariance inputs.');
end
if norm(Ca - Ca',inf) > eps
	warning('Finance:arith2geom:AsymmetricCovariance', ...
		'Non-symmetric covariance input will be made symmetric.');
	Ca = 0.5*(Ca + Ca');
end
[L, D] = ldl(Ca);
if any(diag(D) < 0)
	error('Finance:arith2geom:InvalidCovariance', ...
		'Non-positive-semidefinite covariance input.');
end
if t < 0 || ~isfinite(t)
	error('Finance:arith2geom:InvalidPeriodicity', ...
		'Invalid relative periodicity of geometric returns.');
end

if t ~= 1
	ma = t*ma;
	Ca = t*Ca;
end

mg = exp(ma + 0.5*diag(Ca));
Cg = (mg*mg') .* (exp(Ca) - 1);
mg = mg - 1;

if flip
	mg = mg';
end
